package com.zj.leetcode.medium.hash;

import java.util.HashMap;

/**
 * @program: algorithm
 * @description: 四数相加 II
 * 454 4sum-ii
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class FourSumIi {
    public static void main(String[] args) {
        Solution solution = new FourSumIi().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            HashMap<Integer, Integer> map = new HashMap<>();

            for (int i = 0; i < nums1.length; i++) {
                for (int j = 0; j < nums2.length; j++) {
                    map.put(nums1[i] + nums2[j], map.getOrDefault(nums1[i] + nums2[j], 0) + 1);
                }
            }


            int ret = 0;
            for (int i = 0; i < nums3.length; i++) {
                for (int j = 0; j < nums4.length; j++) {
                    int key = -(nums3[i] + nums4[j]);
                    if(map.containsKey(key)){
                        ret += map.get(key);
                    }
                }
            }

            return ret;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
